package ui.user;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import o.Authority;
import o.UserType;
import tool.MyButton;
import tool.MyLabel;
import vo.UserVo;
import vo.Vo;
import blservice.user.UserBLService;

public class UpdateDialog extends JDialog{

	private static final long serialVersionUID = -1963613828147501013L;
	protected JTextField name;
	protected UserBLService bl;
	protected UserVo user;
	protected UserVo target;
	protected UserManagementPanel father;
	
	public UpdateDialog(UserManagementPanel father, UserVo user,
			UserBLService bl,UserVo target) {
		this.setContentPane(new JPanel() {

			private static final long serialVersionUID = -8067534092982934270L;

			@Override
			public void paint(Graphics g) {
				this.setBackground(new Color(0, 0, 0, 0));
				Image background = Toolkit.getDefaultToolkit().getImage(
						"Graphics/framebackground.jpg");
				g.drawImage(background, 0, 0, null);
				super.paint(g);
			}
		});

		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.target=target;
		this.user=user;
		this.father=father;
		this.bl=bl;
		this.setBounds(200, 200, 176, 251);
		setTitle("更改权限");
		getContentPane().setLayout(null);
		
		JLabel label = new MyLabel("用户名：");
		label.setBounds(10, 10, 94, 15);
		getContentPane().add(label);
		
		name = new JTextField();
		name.setBounds(10, 35, 138, 21);
		getContentPane().add(name);
		name.setColumns(10);
		name.setText(target.getName());
		name.setEditable(false);
		
		JButton button =  new MyButton("Graphics/Button64-21.png");
		button.setText("确定");
		button.setBounds(10, 178, 64, 21);
		getContentPane().add(button);
		
		JButton button_1 = new MyButton("Graphics/Button64-21.png");
		button_1.setText("取消");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UpdateDialog.this.dispose();
			}
		});
		button_1.setBounds(84, 178, 64, 21);
		getContentPane().add(button_1);
		
		JLabel label_3 = new MyLabel("用户类型：");
		label_3.setBounds(10, 66, 94, 15);
		getContentPane().add(label_3);
		
		JComboBox<String> type = new JComboBox<String>();
		type.setBounds(10, 91, 138, 21);
		getContentPane().add(type);
		type.addItem("管理员");
		type.addItem("库存管理人员");
		type.addItem("进货销售人员");
		type.addItem("财务人员");
		type.addItem("总经理");
		type.setSelectedItem(target.getUserType().name());
		
		JLabel label_4 = new MyLabel("用户权限：");
		label_4.setBounds(10, 122, 94, 15);
		getContentPane().add(label_4);
		
		JComboBox<String> quanxian = new JComboBox<String>();
		quanxian.setBounds(10, 147, 138, 21);
		getContentPane().add(quanxian);
		quanxian.addItem("普通权限");
		quanxian.addItem("最高权限");
		quanxian.setSelectedItem(target.getAuthority().name());
		
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				target.setAuthority(Authority.valueOf((String)quanxian.getSelectedItem()));
				target.setUserType(UserType.valueOf((String)type.getSelectedItem()));
				changeAuthority(target);				
			}
		});
	}

	private void changeAuthority(UserVo vo){
		try {
			vo.setOperator(user.getName());
			vo.setOperation("更改用户权限");
			Vo result=bl.changeAuthority(vo);
			if(result.isSuccessed()){
				JOptionPane.showMessageDialog(this,
						"更改用户权限成功\n" );
				father.showAll();
				this.dispose();
			}else{
				JOptionPane.showMessageDialog(this,
						"更改用户权限失败\n" + result.getFailMessage());
			}
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
	}
}
